package cn.kuick.deal.alert.utils;

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;


/*
需要导入的包compile("org.apache.httpcomponents:httpclient:4.5.2")
*/
public class HttpUtils {
	private static final Log log = LogFactory.getLog(HttpUtils.class);
	
	public static String  doGet(String url){
        HttpClient client =  new HttpClient();
        
        GetMethod method = new GetMethod(url);
        
        String response = "";
        try {
			client.executeMethod(method);
			response = method.getResponseBodyAsString();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return response;
	}
	
	public static String doPost(String url, Map<String,String> params){
        HttpClient client =  new HttpClient();
        
        client.getParams().setContentCharset("UTF-8");
        PostMethod method = new PostMethod(url);
        if(params != null){
        	for (Map.Entry<String, String> entry : params.entrySet()) {  
        		String key = entry.getKey();
        		String value = entry.getValue();
        		method.addParameter(key, value);
        	}
        }
        
        String response = "";
        try {
			client.executeMethod(method);
			response = method.getResponseBodyAsString();
		} catch (IOException e) {
			e.printStackTrace();
		}

        return response;
	}

	public static String doPutRestTemplate(String url, Map<String, String> paramMap){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("Content-type", "application/json");

		JSONObject jsonObject = new JSONObject();
		if(paramMap != null){
			for (Map.Entry<String, String> entry : paramMap.entrySet()){
				jsonObject.put(entry.getKey(), entry.getValue());
			}
		}

		HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), httpHeaders);

		ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, String.class);
		return responseEntity.getBody();
	}

	public static String doPutHttpPut(String url, Map<String, String> paramMap){
		org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create().build();

		HttpPut httpPut = new HttpPut(url);
		httpPut.setHeader("Content-type", "application/json");

		JSONObject jsonObject = new JSONObject();
		if(paramMap != null){
			for (Map.Entry<String, String> entry : paramMap.entrySet()){
				jsonObject.put(entry.getKey(), entry.getValue());
			}
		}
		try {
			StringEntity params = new StringEntity(jsonObject.toString());
			httpPut.setEntity(params);

			HttpResponse httpResponse = httpClient.execute(httpPut);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = bufferedReader.readLine()) != null){
				result.append(line);
			}

			return result.toString();

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String doDeleteRestTemplate(String url, Map<String, String> paramMap){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("Content-type", "application/json");

		JSONObject jsonObject = new JSONObject();
		if(paramMap != null){
			for (Map.Entry<String, String> entry : paramMap.entrySet()){
				jsonObject.put(entry.getKey(), entry.getValue());
			}
		}

		HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), httpHeaders);

		ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, String.class);
		return responseEntity.getBody();
	}
}





















